package cn.com.xinjinfu.pdoulcProduct.manager;

/**
 * Created by android-adt on 2016/1/14.
 */
import java.util.LinkedList;

import android.util.Log;

import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;

import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;

public class UserRefreshManager {

    protected static final String TAG = "UserRefreshManager";
    private static UserRefreshManager instance;
    private LinkedList<Refresh> refreshList;
    private boolean isVerify = false;

    private UserRefreshManager()
    {
        refreshList = new LinkedList<Refresh>();
    }

    /**
     * 是否已登录验证过
     * @return
     */
    public boolean isVerify() {
        return isVerify;
    }

    /**
     * 设置是否已登录验证过
     * @param isVerify
     */
    public void setVerify(boolean isVerify) {
        this.isVerify = isVerify;
    }

//    public void loginVerify()
//    {
//        SharedPreferences pre_tuisong = BaseApplication.getInstance().getApplicationContext().getSharedPreferences("pre_tuisong", 0);
//        String channelId = pre_tuisong.getString("channelId", "");
//        String userId = pre_tuisong.getString("userId", "");
//
//        User user = BaseApplication.getInstance().getUser();
//        Map<String, String> parameters = DataHandler.getParameters("1");
//        parameters.put("name", user.getUsername());
//        parameters.put("pwd",user.getPwd());
//        parameters.put("deviceType", "1");
//        parameters.put("channelId", channelId);
//        parameters.put("userId", userId);
//
//        RequestQueue requen = Volley.newRequestQueue(BaseApplication.getInstance().getApplicationContext());
//        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters),null,refreshListener,error);
//        requen.add(request);
//    }

//    private Listener<JSONObject> refreshListener = new Listener<JSONObject>() {
//        @Override
//        public void onResponse(JSONObject jobj) {
//            Log.i(TAG, jobj.toString());
//            if(JSONManager.getCode(jobj)==-1){
//                try {
//                    jobj.put("pwd",BaseApplication.getInstance().getUser().getPwd());
//                } catch (JSONException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//                User user = JSON.parseObject(jobj.toString(),User.class);
//                BaseApplication.getInstance().setUser(user);
//                //更新存入数据
//                SharedPreferences preferences = BaseApplication.getInstance().getApplicationContext().getSharedPreferences(
//                        ConstantManager.USER_INFO, Context.MODE_PRIVATE);
//                Editor editor = preferences.edit();
//                editor.putString(ConstantManager.USER_INFO, jobj.toString());
//                editor.commit();
//            }
//            refresh(JSONManager.getError(jobj),JSONManager.getMsg(jobj));
//        }
//    };

    private ErrorListener error = new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG,"验证失败");
            refresh(ConstantManager.ERROR, DataHandler.getErrorMsg(error));
        }
    };


    /**
     * 添加用户更新视图
     * @param context
     */
    public void addView(Refresh refresh)
    {
        refreshList.add(refresh);
    }

    /**
     * 删除用户视图
     * @param context
     */
    public void removeView(Refresh refresh)
    {
        for (Refresh item : refreshList) {
            if(item == refresh){
                refreshList.remove(item);
            }
        }
    }

    /**
     * 更新用户信息
     * @param context
     */
    public void refresh(int error,String msg)
    {
        for (Refresh item : refreshList) {
            item.refresh(error,msg);
        }
    }

    public static UserRefreshManager getInstance(){
        if(instance == null){
            instance = new UserRefreshManager();
        }
        return instance;
    }

    public interface Refresh {
        public void refresh(int error,String msg);
    }


}
